/**
 * UDP 客户端
 */

import dgram from 'dgram'
import { Buffer } from 'node:buffer'

const client = dgram.createSocket('udp4')

client.on('listening', () => {
  console.log('客户端启动');
})

const msg = Buffer.from('你好呀')


client.send(msg, 0, msg.length, 8080, 'localhost')


client.on('message', (msg, rinfo) => {
  console.log('已经接收到服务端的内容');
  console.log(msg);

  console.log(msg.toString());
  console.log(rinfo);

  // 发送完消息，就断开连接
  client.close()

})

client.on('close', () => {
  console.log('客户端连接，已断开');
})

client.on('error', (err) => {
  console.log('客户端发生错误');
  console.log(err);
})

